{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  [2, 3, 4]\n",
      "b:  [2, 3, 4]\n",
      "c:  [2, 3, 4]\n",
      "a:  [2, 100, 4]\n",
      "b:  [2, 3, 4]\n",
      "c:  [2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "\n",
    "a = [2,3,4]\n",
    "b = a.copy()\n",
    "c = copy.copy(a)\n",
    "print('a: ', a)\n",
    "print('b: ', b)\n",
    "print('c: ', c)\n",
    "\n",
    "a[1] = 100\n",
    "print('a: ', a)\n",
    "print('b: ', b)\n",
    "print('c: ', c)     # 浅拷贝，新建了一个对象\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  [2, [1, 2, 3], 4]\n",
      "b:  [2, [1, 2, 3], 4]\n",
      "c:  [2, [1, 2, 3], 4]\n",
      "a:  [2, [100, 2, 3], 4]\n",
      "b:  [2, [100, 2, 3], 4]\n",
      "c:  [2, [100, 2, 3], 4]\n"
     ]
    }
   ],
   "source": [
    "a = [2,[1,2,3],4]  # 浅拷贝\n",
    "b = a.copy()\n",
    "c = copy.copy(a)\n",
    "print('a: ', a)\n",
    "print('b: ', b)\n",
    "print('c: ', c)\n",
    "\n",
    "a[1][0] = 100\n",
    "print('a: ', a)\n",
    "print('b: ', b)\n",
    "print('c: ', c)     # 浅拷贝， 只新建了对象，其底层属性只拷贝了引用\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:  [2, [1, 2, 3], 4]\n",
      "b:  [2, [1, 2, 3], 4]\n",
      "a:  [2, [100, 2, 3], 4]\n",
      "b:  [2, [1, 2, 3], 4]\n"
     ]
    }
   ],
   "source": [
    "a = [2,[1,2,3],4]  # 深拷贝\n",
    "b = copy.deepcopy(a)\n",
    "print('a: ', a)\n",
    "print('b: ', b)\n",
    "\n",
    "a[1][0] = 100\n",
    "print('a: ', a)\n",
    "print('b: ', b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原对象 id(a)  :  4512324768\n",
      "原对象 id(a.b):  4512324672\n",
      "浅拷贝 id(b)  :  4521977984\n",
      "浅拷贝 id(b.b):  4512324672\n",
      "深拷贝 id(c)  :  4521978512\n",
      "深拷贝 id(c.b):  4521979616\n"
     ]
    }
   ],
   "source": [
    "class A():\n",
    "    def __init__(self) -> None:\n",
    "        self.b = B()\n",
    "class B():\n",
    "    pass\n",
    "\n",
    "a = A()\n",
    "b = copy.copy(a)\n",
    "c = copy.deepcopy(a)\n",
    "print('原对象 id(a)  : ', id(a))\n",
    "print('原对象 id(a.b): ', id(a.b))\n",
    "print('浅拷贝 id(b)  : ', id(b))     # 对象改变，属性不变\n",
    "print('浅拷贝 id(b.b): ', id(b.b))\n",
    "print('深拷贝 id(c)  : ', id(c))     # 对象和对象的属性都改变了\n",
    "print('深拷贝 id(c.b): ', id(c.b))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('opencv')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b04e532e6ce11d72ca7f2113c964d7a5531c18c5e6d3e97b1c42c6f9a3724305"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
